
package com.bh.framework.network;

import java.util.List;

import android.content.Context;
import android.location.LocationManager;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.telephony.TelephonyManager;

/**
 * 网络请求
 * 
 * @author
 * 
 */
public class NetRequest extends BaseRequest
{

	private Context context;
	private static NetRequest netRequest;

	public synchronized static NetRequest getInstance(Context context)
	{
		if (netRequest == null)
		{
			netRequest = new NetRequest(context);
		}
		return netRequest;
	}

	private NetRequest(Context context)
	{
		super();
		this.context = context;
	}

	// 判断gprs是否打开
	public boolean isGpsEnabled()
	{
		LocationManager locationManager = ((LocationManager) context
				.getSystemService(Context.LOCATION_SERVICE));
		List<String> accessibleProviders = locationManager.getProviders(true);
		return accessibleProviders != null && accessibleProviders.size() > 0;
	}

	// 判断wifi是否打开
	public boolean isWifiEnabled()
	{
		ConnectivityManager mgrConn = (ConnectivityManager) context
				.getSystemService(Context.CONNECTIVITY_SERVICE);
		TelephonyManager mgrTel = (TelephonyManager) context
				.getSystemService(Context.TELEPHONY_SERVICE);
		return ((mgrConn.getActiveNetworkInfo() != null && mgrConn
				.getActiveNetworkInfo().getState() == NetworkInfo.State.CONNECTED) || mgrTel
				.getNetworkType() == TelephonyManager.NETWORK_TYPE_UMTS);
	}

	// 网络连接是否好用
	public boolean isNetworkConnected()
	{
		ConnectivityManager cm = (ConnectivityManager) context
				.getSystemService(Context.CONNECTIVITY_SERVICE);
		NetworkInfo network = cm.getActiveNetworkInfo();
		if (network != null)
		{
			return network.isAvailable();
		}
		return false;
	}

	// 判断是否为3G网络
	public boolean is3rd(Context context)
	{
		ConnectivityManager cm = (ConnectivityManager) context
				.getSystemService(Context.CONNECTIVITY_SERVICE);
		NetworkInfo networkINfo = cm.getActiveNetworkInfo();
		if (networkINfo != null
				&& networkINfo.getType() == ConnectivityManager.TYPE_MOBILE)
		{
			return true;
		}
		return false;
	}

	// 判断是wifi还是3g网络
	public boolean isWifi(Context context)
	{
		ConnectivityManager cm = (ConnectivityManager) context
				.getSystemService(Context.CONNECTIVITY_SERVICE);
		NetworkInfo networkINfo = cm.getActiveNetworkInfo();
		if (networkINfo != null
				&& networkINfo.getType() == ConnectivityManager.TYPE_WIFI)
		{
			return true;
		}
		return false;
	}
}
